﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.ETL
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class CopyDbOutput:ResultBase
    {
        #region props
        private int _TblCount;
        /// <summary></summary>
        public int TblCount
        {
            get
            {
                return this._TblCount;
            }
            set
            {
                this._TblCount = value;
            }
        }

        private int _TotalRecordCount;
        /// <summary></summary>
        public int TotalRecordCount
        {
            get
            {
                return this._TotalRecordCount;
            }
            set
            {
                this._TotalRecordCount = value;
            }
        }

        private int _IndexCount;
        /// <summary></summary>
        public int IndexCount
        {
            get
            {
                return this._IndexCount;
            }
            set
            {
                this._IndexCount = value;
            }
        }

        private int _RefCount;
        /// <summary></summary>
        public int RefCount
        {
            get
            {
                return this._RefCount;
            }
            set
            {
                this._RefCount = value;
            }
        }
        #endregion

        #region ctor
        /// <summary>
        /// 
        /// </summary>
        public CopyDbOutput():base(Guid.Empty,Guid.Empty)
        {
            this._TblCount = 0;
            this._TotalRecordCount = 0;
            this._IndexCount = 0;
            this._RefCount = 0;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="componentID"></param>
        /// <param name="jobTicketID"></param>
        /// <param name="tblCount"></param>
        /// <param name="recCount"></param>
        /// <param name="idxCount"></param>
        /// <param name="refCount"></param>
        public CopyDbOutput(Guid componentID, Guid jobTicketID,
            int tblCount, int recCount,int idxCount, int refCount) 
            : base(componentID, jobTicketID)
        {
            this._TblCount = tblCount;
            this._TotalRecordCount = recCount;
            this._IndexCount = idxCount;
            this._RefCount = refCount;
        }
        #endregion

        #region Overrides of ResultBase

        /// <summary>
        /// 
        /// </summary>
        /// <param name="xDoc"></param>
        /// <param name="parentNode"></param>
        /// <returns></returns>
        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Output");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FinishTime", this.FinishTime.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TableCount", this.TblCount.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode,"RecordCount",this.TotalRecordCount.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "IndexCount", this.IndexCount.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ReferenceCount", this.RefCount.ToString());
            return xNode;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="xNode"></param>
        /// <returns></returns>
        public override ResultBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                CopyDbOutput output = new CopyDbOutput();
                // output.OwnerComponentID = new Guid(xNode.Attributes.GetNamedItem("ComponentID").Value);
                output.OwnerWorkerInstanceID = new Guid(xNode.Attributes.GetNamedItem("WorkerID").Value);
                output.FinishTime = DateTime.Parse(xNode.Attributes.GetNamedItem("FinishTime").Value);

                output.TblCount = int.Parse(XmlDataUtil.GetAttributeValue(xNode, "TableCount", "0"));
                output.TotalRecordCount = int.Parse(XmlDataUtil.GetAttributeValue(xNode, "RecordCount", "0"));
                output.IndexCount = int.Parse(XmlDataUtil.GetAttributeValue(xNode, "IndexCount", "0"));
                output.RefCount = int.Parse(XmlDataUtil.GetAttributeValue(xNode, "ReferenceCount", "0"));

                return output;
            }
            else
            {
                throw new Exception("Unable to instantiate output: invalid type specified in xml node");
            }
        }

        #endregion
    }
}
